﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Globalization;

namespace PimImporter
{
    /// <summary>
    /// This is a static class containing default value
    /// </summary>
    internal static class Def
    {
        private static AssemblyName assName = Assembly.GetCallingAssembly().GetName();
        internal static readonly string appName = assName.Name + " " + assName.Version.Major + "." + assName.Version.Minor;
        internal static readonly string xmlFileMapping = Path.GetDirectoryName(assName.CodeBase) + Path.DirectorySeparatorChar + "mapping.xml";
        internal static readonly string errorFile = Path.GetDirectoryName(assName.CodeBase) + Path.DirectorySeparatorChar + "error.txt";
        private const int defaultRes = 96;

        internal static int ResizeResolution(int value, int resolution)
        {
            if (resolution != defaultRes)
                return decimal.ToInt32(value * decimal.Divide(resolution, defaultRes));
            else
                return value;
        }

        internal static DateTime GetDate(string date)
        {
            return DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        }

        internal static void LogErr(Exception ex, string err)
        {
            StreamWriter stream = null;
            try
            {
                if (!File.Exists(errorFile))
                {
                    stream = File.CreateText(errorFile);
                }
                else
                {
                    stream = File.AppendText(errorFile);
                }
                string msg = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + "\r\n"
                    + "Exception: " + ex.GetType().ToString()
                    + "Error Message: " + ex.Message + "\r\n"
                    + "Stack Trace: " + ex.StackTrace + "\r\n"
                    + err + "\r\n";
                stream.WriteLine(msg);
                stream.Flush();
                stream.Close();
            }
            catch { }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
    }
}
